import numpy as np
import pandas as pd
from pandas import Series,DataFrame

df = DataFrame({
    'key1':['a', 'a', 'b','b', 'a'],
    'key2':['one', 'two', 'one', 'two', 'one'],
    'data1':np.random.randn(5),
    'data2':np.random.randn(5)
})

print df
grouped = df['data1'].groupby(df['key1'])
# print grouped
print grouped.mean()
means = df['data1'].groupby([df['key1'], df['key2']]).mean()
print means
print means.unstack()

states = np.array(['Ohio', 'California', 'California', 'Ohio', 'Ohio'])
years = np.array([2005, 2005, 2006, 2005, 2006])
print df['data1'].groupby([states, years]).mean()


print "-------------------1-----------------------"
people = DataFrame(np.random.randn(5, 5),
                   columns=['a', 'b', 'c', 'd', 'e'],
                   index=['Joe', 'Steve', 'Wes', 'Jim', 'Travis'])
people.ix[2:3, ['b', 'c']] = np.nan

print people
mapping = {
    'a':'red', 'b':'red', 'c':'blue',
    'd':'blue', 'e':'red', 'f':'orange'
}
by_column = people.groupby(mapping, axis=1)
print by_column.mean()
print people.groupby(len).sum()

print "----------------2----------------------"
print df
grouped = df.groupby('key1')
print grouped['data1'].quantile(0.9)

def peak_to_peak(arr):
    return arr.max() -arr.min()

print grouped.agg(peak_to_peak)

print "-----------------3----------------------"
tips = pd.read_csv("../pydata-book/examples/tips.csv")
tips['tip_pct'] = tips['tip'] / tips['total_bill']
tips['sex'] = tips['total_bill'].apply(lambda x: 'Male' if int(x)%2>0 else 'Female')
print tips[:6]
grouped = tips.groupby(['sex', 'smoker'])
grouped_pct = grouped['tip_pct']
print grouped_pct.agg('mean')
print grouped_pct.agg(['mean', 'std', peak_to_peak])

print "--------------------4-----------------------"
print df
k1_means = df.groupby('key1').mean().add_prefix('mean_')
print k1_means
print pd.merge(df, k1_means, left_on='key1', right_index=True)
key = ['one', 'two', 'one', 'two', 'one']
print people
print people.groupby(key).mean()
print people.groupby(key).min()
print people.groupby(key).transform(np.mean)

def demean(arr):
    return arr - arr.mean()

demeaned = people.groupby(key).transform(demean)
print demeaned

print demeaned.groupby(key).mean()

print "--------------------5----------------------"
def top(df, n=5, column='tip_pct'):
    # return df.sort_index(by=column)[-n:0]
    return df.sort_values(by=column)[-n:]
print tips
print top(tips, n=6)
print tips.groupby('smoker').apply(top)
print tips.groupby(['smoker', 'day']).apply(top, n=1, column='total_bill')
result =  tips.groupby('smoker')['tip_pct'].describe()
print result
print result.unstack()
